package zjut.raymond.witdemo.algorithm;

import java.util.LinkedList;
import java.util.Queue;
import java.util.StringJoiner;

public class STDCalculator {
    private Queue<Double> mQueue;
    private int mWindow;
 //   private double sum;

    public STDCalculator(int window) {
        mQueue = new LinkedList<>();
        mWindow = window;

    }

    public double getStd(double d) {
        if (mQueue.size() >= mWindow) mQueue.poll();
        mQueue.offer(d);
        double mean = getSum() / mQueue.size();
        double variance = 0;
        for (double data : mQueue) {
            variance += Math.pow((data - mean), 2);
        }
        return Math.sqrt(variance / mQueue.size());
    }

    public Queue<Double> getmQueue() {
        return mQueue;
    }

    public void setmQueue(Queue<Double> mQueue) {
        this.mQueue = mQueue;
    }

    public int getmWindow() {
        return mWindow;
    }

    public void setmWindow(int mWindow) {
        this.mWindow = mWindow;
    }

    public double getSum() {
        int sum = 0;
        for (double data : mQueue) {
            sum += data;
        }
        return sum;
    }

    public String display() {
        StringJoiner stringJoiner = new StringJoiner(",","[","]");
        for (double data: mQueue) {
            stringJoiner.add(String.valueOf(data));
        }
        return stringJoiner.toString();
    }

}

